package com.example.neteaseapplication.ui.discovery;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.neteaseapplication.R;
import com.example.neteaseapplication.entity.CustomViewsInfo;
import com.example.neteaseapplication.util.RoundTransform;
import com.stx.xhb.androidx.XBanner;
import com.stx.xhb.androidx.transformers.Transformer;

import java.util.ArrayList;
import java.util.List;

public class DiscoveryFragment extends Fragment {

    private XBanner mXbanner;
    final List<CustomViewsInfo> mImgUrl = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_discovery, container, false);
//        View view = LayoutInflater.from(getContext()).inflate(R.layout.fg_discovery, null);
        initImg();
        mXbanner = view.findViewById(R.id.xbanner_view);
        mXbanner.setBannerData(R.layout.banner_img,mImgUrl);
//        mXbanner.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ScreenUtil.getScreenWidth(getContext()) / 2));
        initBanner();
        return view;

    }

    public void initBanner(){
        mXbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                Toast.makeText(getContext(), "点击了第" + (position + 1) + "图片", Toast.LENGTH_SHORT).show();
            }
        });
        mXbanner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                //glide请求网络图片
                RequestOptions options = new RequestOptions().centerCrop().transform(new RoundTransform(getActivity(),10));
                Glide.with(getActivity()).load(mImgUrl.get(position).getmInfo()).apply(options).into((ImageView)view);
//                    view.setBackgroundResource(mImgUrl.get(position).getmInfo());
            }
        });
        mXbanner.setPageChangeDuration(3000);

        // 设置XBanner的页面切换特效，有多个，其他的可以到网上去查
        mXbanner.setPageTransformer(Transformer.Default);//横向移动

//        mXbanner.setPageTransformer(Transformer.Alpha); //渐变，效果不明显

        mXbanner.setAutoPlayAble(true);   //设置自动轮播

        mXbanner.setAutoPalyTime(5000);   //图片轮播事件间隔,int类型，默认5000ms
    }

    /**
     * 存入图片网络地址
     */
    public void initImg(){
        mImgUrl.add(new CustomViewsInfo("http://p1.music.126.net/Gr6cM00vDukCftJw9StYbg==/109951164621840537.jpg?imageView&quality=89"));
        mImgUrl.add(new CustomViewsInfo("http://p1.music.126.net/fEwEQguzB_33tKBjYaH3VQ==/109951164621415394.jpg?imageView&quality=89"));
        mImgUrl.add(new CustomViewsInfo("http://p1.music.126.net/FMAEcIZN_C1yos4CjPm3gA==/109951164620217415.jpg?imageView&quality=89"));
        mImgUrl.add(new CustomViewsInfo("http://p1.music.126.net/FuKcM0lGOtdrCZ1gvqDRhw==/109951164621412292.jpg?imageView&quality=89"));
    }
    @Override
    public void onResume() {
        super.onResume();
        mXbanner.startAutoPlay();
    }


    @Override
    public void onStop() {
        super.onStop();
        mXbanner.stopAutoPlay();
    }

    }
